#!/bin/bash
basefun=`which basefunction.sh`
parseIni=`which parseini.sh`
relCfgFile=`which fs.product-qcom-release.ini`
manualSvnInfoFile=".fs.svn.version"
manualSvnVersion=""
appSvnVerFile=".appSvnVer.info"
if [ "$basefun" != "" ] && [ "$parseIni" != "" ] ;then
	. $basefun
else
	echo -e "cant find basefunctions.sh or parseini.sh\n exit ..."
	exit 1
fi
selProject=""

function ShowUsage()
{
	echo -e "fs.pack-qcom-relese.sh --selproject YOUR_PROJECT_ID --rffileid QCN_FILE_NO. --usedefsvnver USE_DEFAULT_SVN_VERSION[yes/no] --copyimages AUTO_COPY_IMAGES[yes/no] --tarimages AUTO_TAR_IMAGES[yes/no] --tarsymbols AUTO_TAR_IMAGES[yes/no] --checkfiles CHECK_HAVE_ALL_FILE[yes/no]"
	exit
}

function GenReleaseText(){
	local relDir="$1"
	echo "projectID:  $projectID
Hardware Version: $swVersion
Software Version：$hwVersion" > $relDir/releaseNote.txt
	if [ "$androidTrunkDir" != "" ]; then
	cat >> $relDir/releaseNote.txt <<EOF
#------- $projectID 主线 信息 -------# 
主线img发布文件: ${trunkTarName}.tar.gz
主线SVN路径:	${trunkRepoUrl}	版本: r${trunkSvnVer}
modem路径:	${modemRepoUrl}		版本: r${modemSvnVer}

EOF
	fi
	
	if [ "$androidBranchDir" != "" ]; then
	cat >> $relDir/releaseNote.txt <<EOF
#------- $projectID 分支 信息 -------# 
分支线img发布文件: ${branchTarName}.tar.gz
分支SVN路径:	${branchRepoUrl}	版本: r${branchSvnVer}
modem路径:	${modemRepoUrl}		版本: r${modemSvnVer}

EOF
	fi
	
#echo "QCN : $selRadioCfgFile" >> $relDir/releaseNote.txt
	
}
# ### DO NOT EDIT FOLLOWING VARS  ###
selVendor=""
selProject=""
rfFileID=""
useDefaultSvnVer=""
isCopyImages=""
isTarImages=""
isTarSymbols=""
isCheckFiles=""
# ### DO NOT EDIT VARS UPON       ###
#allVendor=(`$parseIni "field" "$relCfgFile" "CONFIGS" "allVendor" `)
selVendor="Qualcom"
while [ $# != 0 ];do
    case $1 in
		--selproject)
			shift
			if [ "$1" == "" ];then
				EchoText "未指定项目!" 
				ShowUsage
				exit 
			fi
			selProject="$1"		
		;;
		--rffileid)
			shift
			if [ "$1" == "" ];then
				EchoText "请指定待合并的版本!" 
				ShowUsage
				exit 
			fi
			rfFileID="$1"
		;;
		--usedefsvnver)
 			shift
			if [ "$1" == "" ];then
				EchoText "请指定是否使用默认SVN版本号!" 
				ShowUsage
				exit 
			fi
			useDefaultSvnVer="$1"
		;;
		--copyimages)
			shift
			if [ "$1" == "" ];then
				EchoText "请至定是否拷贝IMG文件!" 
				ShowUsage
				exit 
			fi
			isCopyImages="$1"
		;;
		--tarimages)
			shift
			if [ "$1" == "" ];then
				EchoText "请指定是否压缩IMG文件!" 
				ShowUsage
				exit 
			fi
			isTarImages="$1"
		;;
		--tarsymbols)
			shift
			if [ "$1" == "" ];then
				EchoText "请指定是否压缩symbols文件!" 
				ShowUsage
				exit 
			fi
			isTarSymbols="$1"
		;;
		--checkfiles)
			shift
			if [ "$1" == "" ];then
				EchoText "请指定是否检查存在所有文件!" 
				ShowUsage
				exit 
			fi
			isCheckFiles="$1"
		;;
		-h|--help)
			ShowUsage
			;;
		*)
		;;
    esac
    shift
done

# select vendor and product
allProject=(`$parseIni "field" "$relCfgFile" "CONFIGS" "Qualcom_Projects"` )
if [ "$selProject" == "" ]; then
	selProject="`GetChoice "请选择待打包的项目:" allProject 1`"
fi
# get paramenters 
projectID="`$parseIni "field" "$relCfgFile" "$selProject" "projectID"`"
platform="`$parseIni "field" "$relCfgFile" "$selProject" "platform"`"
projectDir="`$parseIni "field" "$relCfgFile" "$selProject" "projectDir"`"
#emmcboot="`$parseIni "field" "$relCfgFile" "$selProject" "emmcboot"`"
androidTrunkDir="`$parseIni "field" "$relCfgFile" "$selProject" "androidTrunkDir"`"
androidBranchDir="`$parseIni "field" "$relCfgFile" "$selProject" "androidBranchDir"`"
androidImgDir="`$parseIni "field" "$relCfgFile" "$selProject" "androidImgDir"`"
androidImgs="`$parseIni "field" "$relCfgFile" "$selProject" "androidImgs"`"
androidBuildID="`$parseIni "field" "$relCfgFile" "$selProject" "androidBuildID"`"
androidBootloaders="`$parseIni "field" "$relCfgFile" "$selProject" "androidBootloaders"`"
androidUpdateZip="`$parseIni "field" "$relCfgFile" "$selProject" "androidUpdateZip"`"
androidPackZip="`$parseIni "field" "$relCfgFile" "$selProject" "androidPackZip"`"
modemDir="`$parseIni "field" "$relCfgFile" "$selProject" "modemDir"`"
modemImgDir="`$parseIni "field" "$relCfgFile" "$selProject" "modemImgDir"`"
modemImgs="`$parseIni "field" "$relCfgFile" "$selProject" "modemImgs"`"
modemBuildID="`$parseIni "field" "$relCfgFile" "$selProject" "modemBuildID"`"
modemPartitionDir="`$parseIni "field" "$relCfgFile" "$selProject" "modemPartitionDir"`"
modemPartitionImgs="`$parseIni "field" "$relCfgFile" "$selProject" "modemPartitionImgs"`"
modemPartitionOutImgDir="$projectDir/$modemDir/$modemPartitionDir"
releaseDir="`$parseIni "field" "$relCfgFile" "$selProject" "releaseDir"`"
radioCfgFileDir="`$parseIni "field" "$relCfgFile" "$selProject" "radioCfgFileDir"`"
androidObjFiles="`$parseIni "field" "$relCfgFile" "$selProject" "androidObjFiles"`"
modemObjFiles="`$parseIni "field" "$relCfgFile" "$selProject" "modemObjFiles"`"

today=`date +%Y%m%d`
hwVersion=`GetHardwareVersion $projectDir/$androidTrunkDir $projectID`
swVersion=`GetSoftwareVersion $projectDir/$androidTrunkDir $projectID`

# get svn version 
if [ "$androidTrunkDir" != "" ]; then
	trunkRepoUrl="`GetDirRepoUrl $projectDir/$androidTrunkDir`"
	trunkSvnCommitVer="`GetSvnCommitted $projectDir/$androidTrunkDir`"
	if [ -f $projectDir/$androidTrunkDir/$manualSvnInfoFile ]; then
		trunkManualSvnVersion="`cat $projectDir/$androidTrunkDir/$manualSvnInfoFile | grep "svnver=" | awk -F "=" '{print $2}'`"
	fi

	if [ "$trunkManualSvnVersion" != "" ]; then
		trunkSvnCommitVer=$trunkManualSvnVersion
	fi

	trunkHead="`GetRepoHeadVersion $trunkRepoUrl`"
	trunkSvnVer="`InputNumber "请确定主线SVN版本号:"  1 $trunkHead $trunkSvnCommitVer "$useDefaultSvnVer"`"
fi

if [ "$androidBranchDir" != "" ]; then
	branchRepoUrl="`GetDirRepoUrl $projectDir/$androidBranchDir`"
	branchSvnCommitVer="`GetSvnCommitted $projectDir/$androidBranchDir`"
	branchHead="`GetRepoHeadVersion  $branchRepoUrl`"
	branchSvnVer="`InputNumber "请确定分支SVN版本号:"  1 $branchHead $branchSvnCommitVer "$useDefaultSvnVer"`"
fi

if [ "$modemDir" != "" ]; then
	modemRepoUrl=`GetDirRepoUrl $projectDir/$modemDir`
	modemSvnCommitVer="`GetSvnCommitted $projectDir/$modemDir`"
	modemHead="`GetRepoHeadVersion $modemRepoUrl`"
	modemSvnVer="`InputNumber "请确定Modem SVN版本号:"  1 $modemHead $modemSvnCommitVer "$useDefaultSvnVer"`"
fi
trunkAppSvnVer="`cat $projectDir/$androidTrunkDir/$appSvnVerFile 2> /dev/null`"
branchAppSvnVer="`cat $projectDir/$androidBranchDir/$appSvnVerFile 2> /dev/null`"


#  set img dirs and tarFile name
trunkOutImgDir="$projectDir/$androidTrunkDir/$androidImgDir"
branchOutImgDir="$projectDir/$androidBranchDir/$androidImgDir"
modemOutImgDir="$projectDir/$modemDir/$modemImgDir"

trunkRelImgDir="$releaseDir/images-${hwVersion}/image${today}-${swVersion}-trunk"
trunkRelModemDir="$trunkRelImgDir"
trunkRelObjDir="$releaseDir/images-${hwVersion}/image${today}-${swVersion}-trunk-obj"

branchRelImgDir="$releaseDir/images-${hwVersion}/image${today}-${swVersion}-branch"
branchRelModemDir="$branchRelImgDir/$modemBuildID/"

tarRelImgDir="$releaseDir/images-${hwVersion}/image${today}-${swVersion}"
if [ "$trunkAppSvnVer" == "" ]; then
	trunkTarName="${projectID}_${swVersion}_${selVendor}_${platform}_${androidBuildID}_r${trunkSvnVer}_${modemBuildID}_r${modemSvnVer}_trunk"
else
	trunkTarName="${projectID}_${swVersion}_${selVendor}_${platform}_${androidBuildID}_r${trunkSvnVer}_${modemBuildID}_r${modemSvnVer}_app_r${trunkAppSvnVer}_trunk"
fi
trunkObjTarName="image${today}-${swVersion}-trunk-obj"
if [ "$branchAppSvnVer" == "" ]; then
	branchTarName="${projectID}_${swVersion}_${selVendor}_${platform}_${androidBuildID}_r${branchSvnVer}_${modemBuildID}_r${modemSvnVer}_branch"
else
	branchTarName="${projectID}_${swVersion}_${selVendor}_${platform}_${androidBuildID}_r${branchSvnVer}_${modemBuildID}_r${modemSvnVer}_app_r${branchAppSvnVer}_branch"
fi
# sel radio cfg file
# cd $radioCfgFileDir 
# radioCfgFileList=(`ls * 2> /dev/null `)
# cd -
# if [ "$rfFileID" == "" ]; then
# 	selRadioCfgFile="`GetChoice "请选择射频参数文件:" radioCfgFileList 1`"
# else
# 	selRadioCfgFile="`GetChoice "请选择射频参数文件:" radioCfgFileList $rfFileID "yes"`"
# fi
# start to copy 
if [ "$isCopyImages" != "yes" ]; then
isCopyImages=`Question "是否拷贝image文件?" 1 "yes"`
fi
if [ "$isCopyImages" == "yes" ]; then
	# copy trunk images 
	if [ -d "$trunkOutImgDir" ];then
		if [ "$isCheckFiles" == "yes" ]; then
			chkTrunkAnddroidImgs="`CheckFilesExist "$trunkOutImgDir" "$androidImgs"`"
			chkTrunkModemImgs="`CheckFilesExist "$modemOutImgDir" "$modemImgs"`"
			chkTrunkBootloaders="`CheckFilesExist "$trunkOutImgDir" "$androidBootloaders"`"
			chkTrunkModemPartitionImgs="`CheckFilesExist "$modemPartitionOutImgDir" "$modemPartitionImgs"`"
			if [ "$chkTrunkAnddroidImgs" != "yes" ] || [ "$chkTrunkModemImgs" != "yes" ] || [ "$chkTrunkBootloaders" != "yes" ] || [ "$chkTrunkModemPartitionImgs" != "yes" ]; then
				EchoText "经检查需某些需要拷贝的目标文件不存在... chkTrunkAnddroidImgs=$chkTrunkAnddroidImgs chkTrunkModemImgs=$chkTrunkModemImgs chkTrunkBootloaders=$chkTrunkBootloaders"
				exit
			fi
		fi
		rm $trunkRelImgDir/* -rf; # ### clear previous files in any case ###
		CopyFiles "$trunkOutImgDir" "$androidImgs" "$trunkRelImgDir"
		CopyFiles  "$modemOutImgDir" "$modemImgs" "$trunkRelModemDir"
		CopyFiles "$trunkOutImgDir" "$androidBootloaders" "$trunkRelModemDir"
		CopyFiles  "$modemPartitionOutImgDir" "$modemPartitionImgs" "$trunkRelModemDir"
		cd $trunkRelImgDir
		python checksparse.py -i rawprogram0_sparse.xml -s . -o rawprogram0.xml
		rm -fv checksparse.py rawprogram0_sparse.xml*
		cd -
	fi
	# copy branch images
	if [ -d "$branchOutImgDir" ]; then
		if [ "$isCheckFiles" == "yes" ]; then
			chkBranchAnddroidImgs="`CheckFilesExist "$branchOutImgDir" "$androidImgs"`"
			chkBranchModemImgs="`CheckFilesExist "$modemOutImgDir" "$modemImgs"`"
			chkBranchBootloaders="`CheckFilesExist "$branchOutImgDir" "$androidBootloaders"`"
			chkBranchModemPartitionImgs="`CheckFilesExist "$modemPartitionOutImgDir" "$modemPartitionImgs"`"
			if [ "$chkBranchAnddroidImgs" != "yes" ] || [ "$chkBranchModemImgs" != "yes" ] || [ "$chkBranchBootloaders" != "yes" ] || [ "$chkBranchModemPartitionImgs" != "yes" ]; then
 				EchoText "经检查需某些需要拷贝的目标文件不存在... chkBranchAnddroidImgs=$chkBranchAnddroidImgs chkBranchModemImgs=$chkBranchModemImgs chkBranchBootloaders=$chkBranchBootloaders"
				exit
			fi
		fi
		rm $branchRelImgDir/* -rf; # ### clear previous files in any case ###
		CopyFiles "$branchOutImgDir" "$androidImgs" "$branchRelImgDir"
		CopyFiles "$modemOutImgDir" "$modemImgs" "$branchRelModemDir"
		CopyFiles "$branchOutImgDir" "$androidBootloaders" "$branchRelModemDir"
		CopyFiles "$modemPartitionOutImgDir" "$modemPartitionImgs" "$branchRelModemDir"
		#cp "$radioCfgFileDir/$selRadioCfgFile" "$branchRelImgDir" -rfv
	fi
	
fi
# zip files
if [ "$isTarImages" != "yes" ]; then
	isTarImages=`Question "是否创建image压缩文件，进行发布?" 1 "yes"`
fi
if [ "$isTarImages" == "yes" ]; then
	mkdir -p $tarRelImgDir
	rm $tarRelImgDir/* -rf; # ### clear previous files in any case ###

	if [ -d $trunkOutImgDir ]; then
		cp $trunkOutImgDir/$androidUpdateZip $tarRelImgDir/update_${swVersion}_r${trunkSvnVer}_trunk.zip -fv
		trunkPackname=`ls $trunkOutImgDir/$androidPackZip | xargs basename | sed 's/.zip//g'`
		trunkPackdir=`echo $androidPackZip | xargs dirname`
		cp $trunkOutImgDir/$trunkPackdir/$trunkPackname.zip $tarRelImgDir/${trunkPackname}_r${trunkSvnVer}_trunk.zip -fv
	fi
	
	if [ -d $branchOutImgDir ]; then
		cp $branchOutImgDir/$androidUpdateZip $tarRelImgDir/update_${swVersion}_r${branchSvnVer}_branch.zip -fv
		branchPackname=`ls $trunkOutImgDir/$androidPackZip | xargs basename | sed 's/.zip//g'`
		branchPackdir=`echo $androidPackZip | xargs dirname`
		cp $branchOutImgDir/$branchPackdir/$branchPackname.zip $tarRelImgDir/${branchPackname}_r${branchSvnVer}_branch.zip	-fv
	fi
	
	cd $releaseDir/images-${hwVersion}
		if [ -d $trunkRelImgDir ]; then
			mv `basename $trunkRelImgDir` $trunkTarName
			tar -cvzf ${trunkTarName}.tar.gz $trunkTarName
			mv ${trunkTarName}.tar.gz $tarRelImgDir
			mv $trunkTarName `basename $trunkRelImgDir`
			EchoText "计算主线Android和modem img的sha1sum md5sum..." 
			GenChecksum "$trunkRelImgDir $tarRelImgDir"  $tarRelImgDir/${swVersion}-checksum-trunk.info
			EchoText "详见:$tarRelImgDir/${swVersion}-checksum-trunk.info" YELLOW
			EchoText "主线压缩包路径:" GREEN
			EchoText "$tarRelImgDir/${trunkTarName}.tar.gz" RED
		fi
		if [ -d $branchRelImgDir ]; then
			mv `basename $branchRelImgDir` $branchTarName
			tar -cvzf ${branchTarName}.tar.gz $branchTarName
			mv ${branchTarName}.tar.gz $tarRelImgDir
			mv $branchTarName `basename $branchRelImgDir`
			EchoText "计算分支Android和modem img的sha1sum md5sum..." 			
			GenChecksum "$branchRelImgDir  $tarRelImgDir" $tarRelImgDir/${swVersion}-checksum-branch.info
			EchoText "详见:$tarRelImgDir/${swVersion}-checksum-branch.info" YELLOW
			EchoText "分支压缩包路径:" GREEN
			EchoText "$tarRelImgDir/${branchTarName}.tar.gz" RED
 		fi
 		
		if [ "$androidObjFiles" != "" ]; then
			CopyFiles "$trunkOutImgDir" "$androidObjFiles" "$trunkRelObjDir"
		fi
			
		if [ "$modemObjFiles" != "" ]; then
			CopyFiles "$projectDir/$modemDir" "$modemObjFiles" "$trunkRelObjDir"
		fi
		
		if [ -d "$trunkRelObjDir" ]; then
			tar -cvzf $trunkObjTarName.tar.gz `basename $trunkRelObjDir`
			mv $trunkObjTarName.tar.gz $tarRelImgDir/
			rm $trunkRelObjDir -rf
		fi
	cd -
	if [ -d $trunkOutImgDir ]; then
		cp $trunkOutImgDir/$androidUpdateZip $tarRelImgDir/update_${swVersion}_r${trunkSvnVer}_trunk.zip -fv
		trunkPackname=`ls $trunkOutImgDir/$androidPackZip | xargs basename | sed 's/.zip//g'`
		trunkPackdir=`echo $androidPackZip | xargs dirname`
		cp $trunkOutImgDir/$trunkPackdir/$trunkPackname.zip $tarRelImgDir/${trunkPackname}_r${trunkSvnVer}_trunk.zip -fv
	fi
	
	if [ -d $branchOutImgDir ]; then
		cp $branchOutImgDir/$androidUpdateZip $tarRelImgDir/update_${swVersion}_r${branchSvnVer}_branch.zip -fv
		branchPackname=`ls $trunkOutImgDir/$androidPackZip | xargs basename | sed 's/.zip//g'`
		branchPackdir=`echo $androidPackZip | xargs dirname`
		cp $branchOutImgDir/$branchPackdir/$branchPackname.zip $tarRelImgDir/${branchPackname}_r${branchSvnVer}_branch.zip	-fv
	fi
	GenReleaseText $tarRelImgDir
fi
if [ "$isTarSymbols" != "yes" ]; then
	isTarSymbols=`Question "是否备份symbols目录?" 1 "yes"`
fi
if [ "$isTarSymbols" == "yes" ]; then
	if [ -d $trunkOutImgDir ]; then
		echo "备份主线symbols目录"
		cd $trunkOutImgDir
			tar -czf trunk-symbols.tar.gz symbols/
		cd -
		mv $trunkOutImgDir/trunk-symbols.tar.gz $tarRelImgDir/
	fi
# 	if [ -d $branchOutImgDir ]; then
# 		echo "备份分支symbols目录"
# 		cd $branchOutImgDir
# 			tar -czf branch-symbols.tar.gz symbols/
# 		cd -
# 		mv $branchOutImgDir/branch-symbols.tar.gz $tarRelImgDir/
# 	fi
fi


